-module(p55).
-compile(export_all).


main(Bound) ->
    [X || X <- lists:seq(1, Bound),
          is_lychrel_number(X)].

is_lychrel_number(Num) ->
    case is_lychrel_number(Num, 1) of
        {false, _Iter} -> false;
        true -> true
    end.

is_lychrel_number(_Num, 51) -> true;
is_lychrel_number(Num, Iter) ->
    NewNum = Num + reversed_number(Num),
    case is_palindrom(NewNum) of
    	true -> {false, Iter};
        false -> is_lychrel_number(NewNum, Iter + 1)
    end.

is_palindrom(Num) -> Num =:= reversed_number(Num).

reversed_number(Num) ->
    List = erlang:integer_to_list(Num),
    List_reversed = lists:reverse(List),
    erlang:list_to_integer(List_reversed).
